package com.definesys.smecrailtransit.module.MonthForm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.inputmethod.InputMethodManager;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.databinding.DialogEmailBinding;
import com.definesys.smecrailtransit.module.MonthForm.SendEmailListener;
import com.definesys.smecrailtransit.utils.ToastyUtils;

import java.util.HashMap;

/**
 * Created by cjy on 2017/9/16.
 */

public class EmailDialog extends Dialog {
    private int lineId;
    private int applyId;
    private String from;
    private String to;
    private DialogEmailBinding dialogEmailBinding;
    private Context mContext;
    private SendEmailListener listener;


    public EmailDialog(Context context, int themeResId, int lineId, int applyId, String from, String to, SendEmailListener sendEmailListener) {
        super(context,themeResId);
        this.lineId = lineId;
        this.applyId = applyId;
        this.from = from;
        this.to = to;
        this.mContext = context;
        listener=sendEmailListener;
        this.dialogEmailBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_email,null,false);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogEmailBinding.getRoot());
        _initView();
    }

    private void _initView(){
        dialogEmailBinding.submit.setOnClickListener(v -> {
            InputMethodManager inputMethodManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

            inputMethodManager.hideSoftInputFromWindow(EmailDialog.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            if(dialogEmailBinding.email.getText().equals("")){
                ToastyUtils.showWaringToast(mContext,"没有输入邮件地址");
            }else{

                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("email",dialogEmailBinding.email.getText().toString());
                map.put("lineId",lineId);
                map.put("from",from);
                map.put("to", to);
                if(applyId != -1){
                    map.put("applyId",applyId+"");
                }
                listener.sendEmailListener(map);
                dismiss();
            }

            dismiss();


        });

        dialogEmailBinding.cancle.setOnClickListener(v -> {
            dismiss();
        });
    }
}
